﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Threading.Tasks;
using WalkingTec.Mvvm.Core;
using WalkingTec.Mvvm.Mvc;
using WalkingTec.Mvvm.Core.Extensions;
using System.Linq;
using System.Collections.Generic;
using TyMES.Model.BaseInformation;
using TyMES.ViewModel.BaseInformation.LMESBadCodeTemplateVMs;
using TyMES.Model;
using TyMES.Communication;
using TyMES.Services;
using Microsoft.Extensions.Configuration;
using Newtonsoft.Json;

namespace TyMES.BaseInformation.Controllers
{
    [AuthorizeJwtWithCookie]
    [ActionDescription("_Model.LMESBadCodeTemplate")]
    [ApiController]
    [Route("/api/BaseInformation/LMESBadCodeTemplate")]
    public partial class LMESBadCodeTemplateController : BaseApiController
    {
        private readonly HttpService httpService;
        private readonly ProductDataService productDataService;
        private readonly IConfiguration configuration;
        private string _LineCode;
        private string factoryCode;

        public LMESBadCodeTemplateController(HttpService httpService, IConfiguration configuration, ProductDataService productDataService)
        {
            this._LineCode = configuration["LineCode"];
            this.factoryCode = configuration["FactoryCode"];
            this.httpService = httpService;
            this.productDataService = productDataService;   
        }

        [ActionDescription("Sys.Get")]
        [HttpGet("{id}")]
        public IActionResult Get(string id)
        {
            var vm = Wtm.CreateVM<LMESBadCodeTemplateVM>(id);
            return Ok(vm);
        }

        [ActionDescription("Sys.Create")]
        [HttpPost("[action]")]
        public async Task<IActionResult> Create(LMESBadCodeTemplateVM vm)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState.GetErrorJson());
            }
            else
            {
                await vm.DoAddAsync();
   
                if (!ModelState.IsValid)
                {
                    return BadRequest(ModelState.GetErrorJson());
                }
                else
                {
                    return Ok(vm.Entity);
                }
            }

        }

        [ActionDescription("Sys.Edit")]
        [HttpPut("[action]")]
        public async Task<IActionResult> Edit(LMESBadCodeTemplateVM vm)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState.GetErrorJson());
            }
            else
            {
                await vm.DoEditAsync(false);
                if (!ModelState.IsValid)
                {
                    return BadRequest(ModelState.GetErrorJson());
                }
                else
                {
                    return Ok(vm.Entity);
                }
            }
        }
                

        [HttpPost("BatchEdit")]
        [ActionDescription("Sys.BatchEdit")]
        public ActionResult BatchEdit(LMESBadCodeTemplateBatchVM vm)
        {
            if (!ModelState.IsValid || !vm.DoBatchEdit())
            {
                return BadRequest(ModelState.GetErrorJson());
            }
            else
            {
                return Ok(vm.Ids.Count());
            }
        }

		[HttpPost("BatchDelete")]
        [ActionDescription("Sys.Delete")]
        public IActionResult BatchDelete(string[] ids)
        {
            var vm = Wtm.CreateVM<LMESBadCodeTemplateBatchVM>();
            if (ids != null && ids.Count() > 0)
            {
                vm.Ids = ids;
            }
            else
            {
                return Ok();
            }
            if (!ModelState.IsValid || !vm.DoBatchDelete())
            {
                return BadRequest(ModelState.GetErrorJson());
            }
            else
            {
                return Ok(ids.Count());
            }
        }

        [ActionDescription("Sys.DownloadTemplate")]
        [HttpGet("GetExcelTemplate")]
        public IActionResult GetExcelTemplate()
        {
            var vm = Wtm.CreateVM<LMESBadCodeTemplateImportVM>();
            var qs = new Dictionary<string, string>();
            foreach (var item in Request.Query.Keys)
            {
                qs.Add(item, Request.Query[item]);
            }
            vm.SetParms(qs);
            var data = vm.GenerateTemplate(out string fileName);
            return File(data, "application/vnd.ms-excel", fileName);
        }

        [ActionDescription("Sys.Import")]
        [HttpPost("Import")]
        public ActionResult Import(LMESBadCodeTemplateImportVM vm)
        {

            if (vm.ErrorListVM.EntityList.Count > 0 || !vm.BatchSaveData())
            {
                return BadRequest(vm.GetErrorJson());
            }
            else
            {
                return Ok(vm.EntityList.Count);
            }
        }

        [ActionDescription("同步不良模板")]
        [HttpGet("SysncBadTemplate")]
        public async Task<ResponseBase> SysncBadTemplate(string id)
        {
            await productDataService.SyncBadCodeTemplate("");


            return new ResponseBase(true, "同步完成");
        }

    }
}